package com.cheli.managerAsset.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheli.commonutils.R;
import com.cheli.managerAsset.entity.AsAsset;
import com.cheli.managerAsset.entity.AsAssetDescription;
import com.cheli.managerAsset.entity.frontvo.AssetFrontVo;
import com.cheli.managerAsset.entity.vo.AssetInfoVo;
import com.cheli.managerAsset.entity.vo.AssetPublishVo;
import com.cheli.managerAsset.mapper.AsAssetMapper;
import com.cheli.managerAsset.service.AsAssetDescriptionService;
import com.cheli.managerAsset.service.AsAssetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheli.servicebase.config.exceptionhandler.MyException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 资产 服务实现类
 * </p>
 *
 * @author cheli
 * @since 2021-03-28
 */
@Service
public class AsAssetServiceImpl extends ServiceImpl<AsAssetMapper, AsAsset> implements AsAssetService {

    @Autowired
    private AsAssetDescriptionService asAssetDescriptionService;

    @Override
    public String addAssetInfo(AssetInfoVo assetInfoVo) {
       AsAsset asCourse = new AsAsset();
        BeanUtils.copyProperties(assetInfoVo, asCourse);
        int insert = baseMapper.insert(asCourse);
        if(insert == 0 ){
           throw new MyException(20001, "添加资产信息失败");
        }
       String cid = asCourse.getId();


       AsAssetDescription description = new AsAssetDescription();
        BeanUtils.copyProperties(assetInfoVo, description);
       description.setId(cid);
        asAssetDescriptionService.save(description);

        return cid;
    }

    @Override
    public AssetInfoVo getAssetInfo(String id) {
       AsAsset asAsset = baseMapper.selectById(id);

        AssetInfoVo assetInfoVo = new AssetInfoVo();
        BeanUtils.copyProperties(asAsset, assetInfoVo);

       AsAssetDescription description = asAssetDescriptionService.getById(id);
        BeanUtils.copyProperties(description, assetInfoVo);

        return assetInfoVo;
    }

    @Override
    public AssetPublishVo getPublishAssetInfoByAssetId(String id) {
        AssetPublishVo assetPublishVo = baseMapper.getPublishAssetInfoByAssetId(id);
        AsAssetDescription description = asAssetDescriptionService.getById(id);
        BeanUtils.copyProperties(description, assetPublishVo);
        return assetPublishVo;
    }

    @Override
    public void updateAssetById(AssetInfoVo asAssetVo) {
       AsAsset asAsset = new AsAsset();
        BeanUtils.copyProperties(asAssetVo,asAsset);
        int i = baseMapper.updateById(asAsset);
        if(i == 0){
            throw new MyException(20001,"修改课程信息失败");
        }

       AsAssetDescription description = new AsAssetDescription();
        description.setDescription(asAssetVo.getDescription());
        description.setId(asAssetVo.getId());
        asAssetDescriptionService.updateById(description);
    }

   @Override
    public Map<String, Object> getFrontAssetList(Page<AsAsset> page, AssetFrontVo assetFrontVo) {
        QueryWrapper<AsAsset> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(assetFrontVo.getCategoryParentId())){
            wrapper.eq("category_parent_id",assetFrontVo.getCategoryParentId());
        }
        if(!StringUtils.isEmpty(assetFrontVo.getCategoryId())){
            wrapper.eq("category_id",assetFrontVo.getCategoryId());
        }
        if(!StringUtils.isEmpty(assetFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        wrapper.eq("is_out",0);
        baseMapper.selectPage(page, wrapper);
        List<AsAsset> records = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        Map<String, Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }


}
